package com.zmx.patientend.fragment.body_temperature;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.zmx.patientend.R;
import com.zmx.patientend.adapter.BodyListAdapter;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.pojo.body;
import com.zmx.patientend.utils.AllUtil;
import com.zmx.patientend.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 程序猿：胖胖祥
 * 时 间：2020/10/18  12:36
 * 功 能：
 */
public class BodyListFragment extends Fragment {


    private RecyclerView recyclerView;
    private BodyListAdapter adapter;

    private List<body> lists;
    String pid;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_body_list, container, false);

        Bundle bundle =this.getArguments();//得到从Activity传来的数据
        if(bundle!=null){
            pid = bundle.getString("p_id");
        }

        recyclerView = view.findViewById(R.id.review);
        lists = new ArrayList<>();
        adapter = new BodyListAdapter(this.getActivity());
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
        recyclerView.setAdapter(adapter);

        getBts();
        return view;

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:

                    lists.clear();

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        Gson gson = new Gson();

                        if (!object.isNull("data")) {

                            JSONObject data = object.getJSONObject("data");

                            JSONArray array = data.getJSONArray("list");
                            for (int i = 0;i<array.length();i++){

                                body doctor = gson.fromJson(array.getJSONObject(i).toString(),body.class);
                                lists.add(doctor);

                            }

                            adapter.setListAndNotifyDataSetChanged(lists);

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.e("旮都是","放大了看对方"+e.toString());
                    }

                    break;

            }

        }
    };


    public void getBts(){

        List<String> l = new ArrayList<>();
        l.add("");
        l.add(pid);
        l.add("");
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(l)));
        params.put("token", MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getBts");
        params.put("data[patient_id]", pid);
        params.put("data[starttime]", "");
        params.put("data[endtime]", "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/watch", params, handler, 1, 404);

    }


    //签名
    public String sing(List<String> list) {

        StringBuilder requestparams = new StringBuilder();
        requestparams.append(MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.uid, "") +
                MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.role, "") +
                System.currentTimeMillis() / 1000 + MySharedPreferences.getInstance(this.getContext()).getString(MySharedPreferences.password, ""));

        if (list != null) {
            for (String s : list) {
                requestparams.append(s);
            }
        }
        return requestparams.toString();

    }


}
